package com.example.springdemo.Controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.springdemo.entity.Banner;
import com.example.springdemo.service.IBannerService;
import com.example.springdemo.utils.PageResult;
import com.example.springdemo.utils.PassToken;
import com.example.springdemo.utils.ResponseResult;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * bannerController
 * 
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/system/banner")
public class BannerController
{
    @Autowired
    private IBannerService bannerService;

    /**
     * 查询banner列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Banner banner, @RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize)
    {
        // startPage();
        PageResult pageResult = bannerService.selectBannerList(banner,pageNum,pageSize);

        return ResponseResult.createBySuccess(200,pageResult,"查询成功");
    }

    /**
     * 导出banner列表
     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Banner banner)
//    {
//        List<Banner> list = bannerService.selectBannerList(banner);
//        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
//        util.exportExcel(response, list, "banner数据");
//    }

    /**
     * 获取banner详细信息
     */
    @GetMapping(value = "/{id}")
    public ResponseResult getInfo(@PathVariable("id") Long id)
    {
        Banner banner = bannerService.selectBannerById(id);
        return ResponseResult.createBySuccess(200,banner,"查询成功");
    }

    /**
     * 新增banner
     */

    @PostMapping
    public ResponseResult add(@RequestBody Banner banner)
        {
            int i = bannerService.insertBanner(banner);
            if (i > 0) {
                return ResponseResult.createBySuccess(200, "", "添加成功");
            } else {
                return ResponseResult.createByFail(200, "添加失败");
            }
        }


    /**
     * 修改banner
     */

    @PutMapping
    public ResponseResult edit(@RequestBody Banner banner)
    {
        int i = bannerService.updateBanner(banner);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "修改成功");
        } else {
            return ResponseResult.createByFail(200, "修改失败");
        }
    }

    /**
     * 删除banner
     */

	@DeleteMapping("/{ids}")
    public ResponseResult remove(@PathVariable Long[] ids)  //批量删除，单个删除
    {
        int i = bannerService.deleteBannerByIds((ids));
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "删除成功");
        } else {
            return ResponseResult.createByFail(200, "删除失败");
        }
    }
    }

